package com.yonyou.cyxdms.sysAuth.service.controller.manage.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyx.function.utils.security.Md5Util;
import com.yonyou.cyxdms.sysAuth.service.constants.SysAuthCommonConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.user.UserEditPassword;
import com.yonyou.cyxdms.sysAuth.service.entity.JwtInfo;
import com.yonyou.cyxdms.sysAuth.service.entity.cms.GetUserInfoByjwtVO;
import com.yonyou.cyxdms.sysAuth.service.feign.IAuthProviderService;
import com.yonyou.cyxdms.sysAuth.service.service.UserService;
import com.yonyou.cyxdms.sysAuth.service.vo.user.UserInfosVO;
import io.swagger.annotations.*;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author ：quankk
 * @date ：2019/7/18 15:10
 * @description：账号修改控制类
 */
@Api(value = "账号修改控制类Controller", tags = {"账号修改控制类Controller"})
@RestController
@RequestMapping("/basedata/userInfo")
public class UserInfoController extends BaseController {

    // 定义日志接口
    private static final Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private IAuthProviderService authProviderService;

    /**
     * @author ：quankk
     * @date ：2019/7/18 15:11
     * @description：用户密码查询用户信息
     */
    @RequestMapping(method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public IPage<UserInfosVO> searchUserInfos(@ApiParam(name = "pageNum", value = "当前页") @RequestParam("pageNum") Long current,
                                              @ApiParam(name = "limit", value = "分页大小") @RequestParam("limit") Long size,
                                              @RequestParam Map<String, String> param) {
        Page<UserInfosVO> page = new Page(current, size);
        return userService.searchUserInfos(page, param);
    }

    /**
     * @author ：quankk
     * @date ：2019/7/18 15:56
     * @description：修改密码
     */
    @RequestMapping(value = "/editPassword", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "修改密码")
    @ResponseStatus(HttpStatus.CREATED)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public void editPassword(@RequestBody UserEditPassword userEditPassword) {
        userService.editPassword(userEditPassword);
    }

    /**
     * @author ：quankk
     * @date ：2019/7/18 16:19
     * @description：重置密码
     */
    @RequestMapping(value = "/restPassword", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "重置密码")
    @ResponseStatus(HttpStatus.CREATED)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public void restPassword(@RequestBody UserEditPassword userEditPassword) {
        userService.restPassword(userEditPassword);
    }

    /**
     * @author ：李贺
     * @date ：2019/9/24
     * @description：重置密码
     */
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "重置密码")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public void resetPassword(@RequestParam String employeeNo, @RequestParam(value = "passWord", required = false) String passWord) {
        userService.resetPassword(employeeNo, passWord);
    }

    /**
     * 根据jwt获取用户信息
     */
    @RequestMapping(value = "/getUserInfoByjwt", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public GetUserInfoByjwtVO getUserInfoByjwt(@RequestParam("jwt") String jwt) {
        try {
            String nonce = String.valueOf(RandomUtils.nextInt(0, 10));
            String occurtime = String.valueOf(System.currentTimeMillis());
            String data = SysAuthCommonConstants.AUTHORIZATION_CODE + nonce + occurtime;
            String sign = Md5Util.md5(data).toLowerCase();
            JwtInfo jwtInfo = authProviderService.getJwtInfo(jwt, nonce, occurtime, sign).getData();
            logger.info("JTV返回的USERID！"+jwtInfo.getUserId());
            GetUserInfoByjwtVO vo = userService.getuserinfobyuid(jwtInfo.getUserId());
            return vo;
        } catch (
                Exception e)
        {
            logger.info("JWT空！");
            return null;
        }
    }

}
